var key = 'ducktjk319';//用户名密码加密的key
var iv = '0123456789abcdef';
var vue = new Vue({
    el: '#content',
    data: {
        userName: "",
        passWord: ""
    },
    watch: {

    },
    methods: {
        clear: function(e){
            this.userName='';
            this.password='';
        },
        login: function (e) {
            debugger;
            $(".errorMsg").css("top","25%");
            $(".errorMsg").fadeIn("slow");
            $(".errorMsg").css("display","none");
            // `this` 在方法里指当前 Vue 实例
            var userName = (this.userName.trim());
            var passWord = (this.passWord.trim());
            if ($.trim(userName) != null && $.trim(userName) != "") {
                userName = encrypt(userName);
                if (passWord != null && passWord != "") {
                    passWord = encrypt(passWord);
                    $.ajax({
                        url: $.k8sUrl() + 'common/login/login',
                        type: "POST",
                        data: {
                            userName: userName,
                            passWord: passWord
                        },
                        dataType: 'json',
                        success: function (data) {
                            if (data.success && data.code == "0017") {
                                sessionStorage.setItem("fullName", data.data.fullName);
                                localStorage.setItem("userName", userName);
                                localStorage.setItem("passWord", passWord);
                                localStorage.setItem("remember", remember);
                                sessionStorage.setItem("isSx", data.data.isSx);
                                sessionStorage.setItem("isLs", data.data.isLs);
                                sessionStorage.setItem("isSgs", data.data.isSgs);
                                sessionStorage.setItem("isDq", data.data.isDq);
                                sessionStorage.setItem("isHz", data.data.isHz);
                                sessionStorage.setItem("isNewHome", data.data.newHome);
                                sessionStorage.setItem("newHomeOrg", data.data.newHomeOrg);
                                if(data.data.newHome || data.data.isHz){
                                    window.location.replace($.baseUrl() + 'newHome.html');
                                }else{
                                    window.location.replace($.baseUrl() + 'home.html');
                                }
                            } else {
                                $(".errorMsg").html(data.msg);
                                $(".errorMsg").css("top","25%");
                                $(".errorMsg").fadeIn("slow");
                                $(".errorMsg").css("display","flex");
                            }
                        }
                    });
                } else {
                    $(".errorMsg").html("请输入密码！");
                    $(".errorMsg").css("top","25%");
                    $(".errorMsg").fadeIn("slow");
                    $(".errorMsg").css("display","flex");
                }
            } else {
                $(".errorMsg").html("请输入用户名！");
                $(".errorMsg").css("top","25%");
                $(".errorMsg").fadeIn("slow");
                $(".errorMsg").css("display","flex");
            }
        }
    }
});

/**
 * 加密（需要先加载lib/aes/aes.min.js文件）
 * @param word
 * @returns {*}
 */
function encrypt(data) {
    //加密
    var srcs = CryptoJS.enc.Utf8.parse(data)
    var encrypted = CryptoJS.AES.encrypt(srcs, CryptoJS.enc.Utf8.parse(key), {
        iv: CryptoJS.enc.Utf8.parse(iv),
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
    });
    return encrypted.toString();
}